package com.attendance.service;

import com.alibaba.fastjson.JSONObject;
import com.attendance.annotation.Supplement;
import com.attendance.annotation.type.OperationType;
import com.attendance.java.StringUtils;
import com.attendance.mapper.RoleMapper;
import com.attendance.mapper.RoleMenuMapper;
import com.attendance.mapper.UserRoleMapper;
import com.attendance.pojo.Role;
import com.attendance.pojo.RoleMenu;
import com.attendance.pojo.UserRole;
import com.attendance.security.role.pojo.AbstractRole;
import com.attendance.security.role.service.AbstractRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色
 *
 * @author 今朝
 * @date 2021/03/17
 */
@Service
public class RoleServiceImpl implements AbstractRoleService {

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RoleMenuMapper roleMenuMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    public Page<Role> getListPage(Integer pageNum, Integer limit) {
        Page<Role> page = new Page<>(pageNum, limit);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("idx");
        roleMapper.selectPage(page, queryWrapper);
        return page;
    }

    public Page<Role> getListPage(Integer pageNum, Integer limit, String searchParams) {
        JSONObject parse = JSONObject.parseObject(searchParams);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        for (String key : parse.keySet()) {
            queryWrapper.like(key, parse.get(key));
        }
        queryWrapper.orderByAsc("idx");
        Page<Role> page = new Page<>(pageNum, limit);
        roleMapper.selectPage(page, queryWrapper);
        return page;
    }

    public Role selectById(Long id) {
        return roleMapper.selectById(id);
    }

    public List<Long> getMenuIdsById(Long id) {
        return roleMapper.findMenuIdsById(id);
    }

    @Supplement(type = OperationType.UPDATE)
    public Boolean updateById(Role role) {
        return roleMapper.updateById(role) != 0;
    }

    @Supplement(type = OperationType.PUT)
    public Boolean add(Role role, String menuIds) {
        if (roleMapper.insert(role) != 0) {
            for (String id : menuIds.split(",")) {
                roleMenuMapper.insert(new RoleMenu(role.getId(), Long.valueOf(id)));
            }
            return true;
        }
        return false;
    }

    @Supplement(type = OperationType.UPDATE)
    public Boolean updateById(Role role, String menuIds) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<RoleMenu>();
        queryWrapper.eq("role_id", role.getId());
        roleMenuMapper.delete(queryWrapper);
        for (Long id : StringUtils.strToIdsByComma(menuIds)) {
            roleMenuMapper.insert(new RoleMenu(role.getId(), id));
        }
        return roleMapper.updateById(role) != 0;
    }

    public Boolean delete(List<Integer> ids) {
        return ids.size() == roleMapper.deleteBatchIds(ids);
    }

    public Boolean binding(String uIds, String rIds) {
        List<Long> userIds = StringUtils.strToIdsByComma(uIds);
        List<Long> roleIds = StringUtils.strToIdsByComma(rIds);
        for (Long uid : userIds) {
            QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", uid);
            userRoleMapper.delete(queryWrapper);
            for (Long rid : roleIds) {
                userRoleMapper.insert(new UserRole(uid, rid));
            }
        }
        return true;
    }

    @Override
    public List<AbstractRole> getRoleById(Long id) {
        List<AbstractRole> abstractRoles = new ArrayList<>();
        for (Role role : roleMapper.findRolesById(id)) {
            abstractRoles.add(role);
        }
        return abstractRoles;
    }
}
